package com.fgw.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;


import com.fgw.admin.mapper.OrganizationMapper;
import com.fgw.admin.entity.Organization;
import com.fgw.admin.service.IOrganizationService;
import com.fgw.admin.vo.IndustryVo;


@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IOrganizationService {

	@Autowired
	private OrganizationMapper organizationMapper;
	
	@Override
	public List<IndustryVo> getOrgTree() {
		List<IndustryVo> IndustryVoList = getIndustryVo(0);
		
		return IndustryVoList;
	}

	private List<IndustryVo> getIndustryVo(Integer id){
		
		List<IndustryVo> list = new ArrayList<>();
		
		 Wrapper<Organization> wrapper = new EntityWrapper<Organization>();
			
		 wrapper.eq("parent_id", id);
		
		 List<Organization> orgList = organizationMapper.selectList(wrapper);
		 
		 for (int i = 0; i < orgList.size(); i++) {
			
			 Organization org = orgList.get(i);
			 
			 IndustryVo industryVo = new IndustryVo();
			 
			 industryVo.setId(org.getOrgId());
			 industryVo.setTitle(org.getOrgName());
			 
			 List<IndustryVo> newIndustryVoList = getIndustryVo(org.getOrgId());
			 
			 industryVo.setChildren(newIndustryVoList);
			 
			 list.add(industryVo);
			 
		}
		 
		 return list;
		
	}

	@Override
	public List<Organization> selectListByParam(Organization wrapper) {
		
		return organizationMapper.selectByParam(wrapper);
	}
}